import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/src/common/app_colors.dart';
import 'package:finance_app/src/entities/payslips_entity.dart';
import 'package:fl_chart/fl_chart.dart';
import 'package:flutter/material.dart';
import 'dart:math' as math;

class NormalBarTwoWayChart extends StatefulWidget {
  final PayslipsEntity data;

  NormalBarTwoWayChart({Key key, this.data}) : super(key: key);

  @override
  _NormalBarTwoWayChartState createState() => _NormalBarTwoWayChartState();
}

class _NormalBarTwoWayChartState extends State<NormalBarTwoWayChart> {
  List<Color> colors = [
    aceGreenColor,
    aceRedColor,
  ];

  num max = 0;
  num min = 0.0;

  @override
  void initState() {
    super.initState();
    widget.data?.chartData?.forEach((element) {
      max = math.max(element.value, max);
      min = math.min(element.value, min);
    });
  }

  @override
  Widget build(BuildContext context) {
    return AspectRatio(
      aspectRatio: 1.7,
      child: Padding(
        padding:
            EdgeInsets.only(top: getDp(6), left: getDp(11), right: getDp(20)),
        child: BarChart(
          BarChartData(
            maxY: max == 0 && min == 0 ? 12 : null,
            minY: max == 0 && min == 0 ? -12 : null,
            groupsSpace: 24,
            barTouchData: BarTouchData(
              enabled: false,
            ),
            titlesData: FlTitlesData(
              show: true,
              bottomTitles: SideTitles(
                showTitles: true,
                getTextStyles: (_, value) => TextStyle(
                  color: aceBlackColor,
                  fontSize: getSp(12),
                  height: 14 / 12,
                ),
                margin: 10,
                rotateAngle: 0,
                interval: 1,
                getTitles: (double value) {
                  if (widget.data == null) {
                    switch (value.toInt()) {
                      case 0:
                        return 'Sep';
                      case 2:
                        return 'Oct';
                      case 4:
                        return 'Nov';
                      case 6:
                        return 'Dec';
                      case 8:
                        return 'Jan';
                      case 10:
                        return 'Feb';
                      default:
                        return '';
                    }
                  } else {
                    if (value.toInt() % 2 == 0) {
                      return widget.data.chartData[value.toInt() ~/ 2].name;
                    } else {
                      return null;
                    }
                  }
                },
              ),
              leftTitles: SideTitles(
                showTitles: true,
                getTextStyles: (_, value) =>
                    TextStyle(color: aceBlackColor, fontSize: getSp(12)),
                rotateAngle: 0,
                getTitles: (double value) {
                  if (value == 0) {
                    return '0';
                  } else if (value.abs() < 1000) {
                    return '${(value).toStringAsFixed(2)}';
                  } else if (1000 <= value.abs() && value.abs() < 1000000) {
                    return '${(value / 1000).toStringAsFixed(2)}k';
                  } else {
                    return '${(value / 1000000).toStringAsFixed(2)}m';
                  }
                },
                interval: max == 0 && min == 0
                    ? 3
                    : widget.data?.interval?.toDouble() ?? 1000,
                margin: getDp(20),
                reservedSize: getDp(50),
              ),
              rightTitles: SideTitles(showTitles: false),
              topTitles: SideTitles(showTitles: false),
            ),
            gridData: FlGridData(
              show: true,
              checkToShowHorizontalLine: (value) => value % 5 == 0,
              getDrawingHorizontalLine: (value) {
                return FlLine(
                  color: aceWhiteGreyColor,
                  strokeWidth: getDp(1),
                );
              },
            ),
            borderData: FlBorderData(
              show: false,
            ),
            barGroups: widget.data == null
                ? []
                : List.generate(
                        widget.data.chartData.length,
                        (index) => _buildGroupData(index * 2,
                            widget.data.chartData[index].value?.toDouble()))
                    .toList(),
          ),
        ),
      ),
    );
  }

  BarChartGroupData _buildGroupData(int x, double y) => BarChartGroupData(
        x: x,
        barRods: [
          BarChartRodData(
            y: y,
            width: getDp(12),
            colors: [y >= 0 ? colors[0] : colors[1]],
            borderRadius: const BorderRadius.only(
                topLeft: Radius.circular(2), topRight: Radius.circular(2)),
          ),
        ],
      );

  @override
  void didUpdateWidget(covariant NormalBarTwoWayChart oldWidget) {
    super.didUpdateWidget(oldWidget);
    setState(() {
      max = 0;
      widget.data?.chartData?.forEach((element) {
        max = math.max(element.value, max);
      });
    });
  }
}
